<template>
  <div class="trainerBox">
    <el-form
      @submit.native.prevent
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="课程名称" prop="courseIds">
        <el-select
          style="width: 280px"
          v-model="queryParams.courseIds"
          filterable
          clearable
          multiple
          collapse-tags
        >
          <el-option
            v-for="item in courses"
            :key="item.courseId"
            :label="item.courseName"
            :value="item.courseId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学习岗位" prop="coursePostIds">
        <el-select
          v-model="queryParams.coursePostIds"
          filterable
          clearable
          multiple
          collapse-tags
          style="width: 280px"
        >
          <el-option
            v-for="item in posts"
            :key="item.postId"
            :label="item.postName"
            :value="item.postId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店" prop="schoolIds">
        <el-select
          v-model="queryParams.schoolIds"
          filterable
          clearable
          multiple
          collapse-tags
          style="width: 280px"
        >
          <el-option
            v-for="item in schools"
            :key="item.schoolId"
            :label="item.schoolName"
            :value="item.schoolId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="训练员" prop="teaUserIds">
        <el-select
          v-model="queryParams.teaUserIds"
          filterable
          clearable
          multiple
          collapse-tags
          style="width: 280px"
        >
          <el-option
            v-for="(item, index) in teachers"
            :key="index"
            :label="item.nickName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="训练员状态" prop="teaStatus">
        <el-select v-model="queryParams.teaStatus" filterable clearable>
          <el-option label="正常" :value="1"></el-option>
          <el-option label="已撤销" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="训练员设置时间">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="value1"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="训练员撤销时间">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="value2"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
        <el-button
          v-preventReClick
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      height="280"
      border
      v-loading="loading"
      :data="postList"
      ref="tableRef"
    >
      <el-table-column
        show-overflow-tooltip
        label="课程名称"
        align="center"
        prop="courseName"
      >
        <template #header>
          课程名称<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">2024/10/31后开始统计</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="封面"
        align="center"
        prop="coursePic"
      >
        <!-- 封面 -->
        <template slot-scope="{ row }">
          <el-image
            style="width: 120px; height: 50px"
            :src="row.coursePic"
            :preview-src-list="[row.coursePic]"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="学习岗位"
        align="center"
        prop="coursePostNames"
      />
      <el-table-column
        show-overflow-tooltip
        label="门店"
        align="center"
        prop="schoolName"
      />
      <el-table-column
        show-overflow-tooltip
        label="训练员"
        align="center"
        prop="teaName"
      />
      <el-table-column
        show-overflow-tooltip
        label="训练员岗位"
        align="center"
        prop="teaPostNames"
      />
      <el-table-column
        show-overflow-tooltip
        label="训练员状态"
        align="center"
        prop="status"
      >
        训练员状态，1=正常,2=已撤销
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">已撤销</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="训练员设置时间"
        align="center"
        prop="setTime"
      />
      <el-table-column
        show-overflow-tooltip
        label="训练员撤销时间"
        align="center"
        prop="cancelTime"
      />
      <el-table-column
        show-overflow-tooltip
        label="门店管理员"
        align="center"
        prop="managerName"
      >
        <template #header>
          门店管理员<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">操作的门店管理员</div>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        label="门店管理员岗位"
        align="center"
        prop="managerPostNames"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  courseList,
  schoolList,
  postList,
  teacherList,
} from "@/api/screen/courseLearningManagement";
import { setTeacherList } from "@/api/screen/train";
export default {
  props: {
    active: [String, Number],
  },
  data() {
    return {
      loading: false,
      postList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teaUserIds: undefined,
        schoolId: undefined,
        teaStatus: undefined,
        coursePostIds: undefined,
        courseIds: undefined,
        setStartTime: undefined,
        setEndTime: undefined,
        cancelStartTime: undefined,
        cancelEndTime: undefined,
      },
      value1: "",
      value2: "",
      total: 10,
      teachers: [],
      schools: [],
      posts: [],
      courses: [],
    };
  },
  mounted() {},
  watch: {
    active: {
      handler(newVal) {
        console.log("newVal", newVal);
        if (newVal == 1) {
          this.getSchoolList();
          this.getPostList();
          this.getTeacherList();
          this.getCourseList();
          this.getList();
        }
      },
      immediate: true,
    },
  },
  methods: {
    async getTeacherList() {
      let res;
      try {
        res = await teacherList({ pageNum: 1, pageSize: 9999 });
        if (res.code == 200) {
          this.teachers = res.rows;
        }
      } finally {
      }
    },
    async getPostList() {
      let res;
      try {
        res = await postList({ pageNum: 1, pageSize: 9999 });
        if (res.code == 200) {
          this.posts = res.rows;
        }
      } finally {
      }
    },
    async getSchoolList() {
      let res;
      try {
        res = await schoolList({
          pageNum: 1,
          pageSize: 9999,
        });
        if (res.code == 200) {
          this.schools = res.rows;
        }
      } finally {
      }
    },
    async getCourseList() {
      let res;
      try {
        res = await courseList({
          pageNum: 1,
          pageSize: 9999,
          status: 0,
        });
        if (res.code == 200) {
          this.courses = res.rows;
        }
      } finally {
      }
    },
    async getList() {
      let res;
      try {
        this.loading = true;

        this.queryParams.setStartTime = this.value1 ? this.value1[0] : "";
        this.queryParams.setEndTime = this.value1
          ? this.value1[1].replace("00:00:00", "23:59:59")
          : "";
        this.queryParams.cancelStartTime = this.value2 ? this.value2[0] : "";
        this.queryParams.cancelEndTime = this.value2
          ? this.value2[1].replace("00:00:00", "23:59:59")
          : "";

        let params = { ...this.queryParams };
        params.schoolIds =
          params.schoolIds && params.schoolIds.length > 0
            ? params.schoolIds.join(",")
            : "";
        params.coursePostIds =
          params.coursePostIds && params.coursePostIds.length > 0
            ? params.coursePostIds.join(",")
            : "";
        params.teaUserIds =
          params.teaUserIds && params.teaUserIds.length > 0
            ? params.teaUserIds.join(",")
            : "";
        params.courseIds =
          params.courseIds && params.courseIds.length > 0
            ? params.courseIds.join(",")
            : "";

        res = await setTeacherList(params);
        if (res.code == 200) {
          this.postList = res.rows;
          this.total = res.total;
          // 页面数据更新后，让表格滚动条回到顶部
          this.$nextTick(() => {
            if (this.$refs.tableRef) {
              this.$refs.tableRef.bodyWrapper.scrollTop = 0;
            }
          });
        }
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.value1 = "";
      this.value2 = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.setStartTime = this.value1 ? this.value1[0] : "";
      this.queryParams.setEndTime = this.value1
        ? this.value1[1].replace("00:00:00", "23:59:59")
        : "";
      this.queryParams.cancelStartTime = this.value2 ? this.value2[0] : "";
      this.queryParams.cancelEndTime = this.value2
        ? this.value2[1].replace("00:00:00", "23:59:59")
        : "";

      let params = { ...this.queryParams };
      params.schoolIds =
        params.schoolIds && params.schoolIds.length > 0
          ? params.schoolIds.join(",")
          : "";
      params.coursePostIds =
        params.coursePostIds && params.coursePostIds.length > 0
          ? params.coursePostIds.join(",")
          : "";
      params.teaUserIds =
        params.teaUserIds && params.teaUserIds.length > 0
          ? params.teaUserIds.join(",")
          : "";
      params.courseIds =
        params.courseIds && params.courseIds.length > 0
          ? params.courseIds.join(",")
          : "";
      this.download(
        "/screen/teacher/setTeacherExport",
        {
          ...params,
        },
        `setTeacherExport_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.trainerBox {
  ::v-deep .el-table .el-table__header-wrapper tr th .cell {
    font-weight: 700 !important;
  }
}
</style>